﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LotteryVote.Logic.Web;
using LotteryVote.Logic;
using LotteryVote.Entity;
using LotteryVote.Logic.Exceptions;

namespace LotteryVote
{
    public partial class UserPanel : AgentBasePage
    {
        UserFacade userFacade = new UserFacade();
        protected void Page_Load(object sender, EventArgs e)
        {
            InitParams();
        }
        void InitParams()
        {
            lv_userSerach.OnSeach += new EventHandler(lv_userSerach_OnSeach);
            if (!IsPostBack)
                BindUser();
        }

        void BindUser()
        {
            rpt_user.DataSource = GetSupperDataSource();
            rpt_user.DataBind();
        }

        IList<User> GetSupperDataSource()
        {
            if (lv_userSerach.IsSearched)
            {
                return userFacade.FindSuperUser(lv_userSerach.Name, lv_userSerach.UserName, lv_userSerach.UserState, CurrentUser.Role + 1, lv_userSerach.Sort);
            }
            else
                return userFacade.GetChildUsers(CurrentUser);
        }

        public string GetAgentType()
        {
            SystemRole role = CurrentUser.Role + 1;
            return GetGlobalResourceObject("Resource", role.ToString()).ToString();
        }

        void lv_userSerach_OnSeach(object sender, EventArgs e)
        {
            BindUser();
        }

        protected void rpt_user_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                Execute(() =>
                {
                    int userId = Convert.ToInt32(e.CommandArgument);
                    userFacade.DeleteUser(userId);
                    BindUser();
                });
            }
        }
    }
}